package com.linchaoen;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Bird {

    public BufferedImage birdImage;

    public BufferedImage[] birdImages;

    public int x;

    public int y;

    public int height = 48;

    public int width = 56;

    public int index = 0;

    //自由落体相关属性
    public double speed = 0;//向上的瞬时速度
    public double upSpeed = 15;//向上的初速度
    public double t = 0.2;
    public double g = 4;//重力加速度
    public double s = 0;//经过t时间后，相对于t时间前的位移

    public Bird() {
        try {
            x = 100;
            y = 200;
            birdImage = ImageIO.read(getClass().getResource("0.png"));
            //初始化birdImages
            birdImages = new BufferedImage[8];
            for (int i = 0; i < birdImages.length; i++) {
                birdImages[i] = ImageIO.read(getClass().getResource(i + ".png"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 给一个向上的速度
     */
    public void up() {
        speed = upSpeed;
    }

    /**
     * 鸟子飞的方法
     */
    public void fly() {
        index++;
        //模8是不越界，除以8是放慢速度
        birdImage = birdImages[index / 8 % 8];
    }

    /**
     * 自由落体
     */
    public void upAndDown() {
        double v = speed;
        //垂直方向的位移
        s = v * t - g * t * t / 2;
        y = y - (int) s;
        speed = v - g * t;
    }


}
